package com.mycompany.crm.controller;

import com.mycompany.crm.annotation.LogAnno;
import com.mycompany.crm.base.BaseController;
import com.mycompany.crm.base.ResultInfo;
import com.mycompany.crm.query.CustomerContactQuery;
import com.mycompany.crm.service.CustomerContactService;
import com.mycompany.crm.service.CustomerService;
import com.mycompany.crm.vo.Customer;
import com.mycompany.crm.vo.CustomerContact;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/4/24 17:32
 */
@Controller
@RequestMapping("contact")
public class CustomerContactController extends BaseController{

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerContactService customerContactService;

    /**
     * 分页多条件查询客户交往记录列表
     *
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @LogAnno(description = "分页多条件查询客户交往记录列表")
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomerContactByParams(CustomerContactQuery customerContactQuery) {
        return customerContactService.queryCustomerContactByParams(customerContactQuery);
    }

    /**
     * 打开添加交往记录的对话框
     * @param cusId
     * @param model
     * @return
     */
    @LogAnno(description = "打开添加交往记录的对话框")
    @RequestMapping("toAddCustomerContactPage")
    public String toAddCustomerContactPage(Integer cusId, Model model) {
        Customer customer = customerService.selectByPrimaryKey(cusId);
        model.addAttribute("customer",customer);
        return "customer/customer_contact_add";
    }

    /**
     * 打开修改交往记录的对话框
     * @param id
     * @param
     * @return
     */
    @LogAnno(description = "打开修改交往记录的对话框")
    @RequestMapping("toUpdateCustomerContactPage")
    public String toUpdateCustomerContactPage(Integer id, Integer cusId, Model model) {
        Customer customer = customerService.selectByPrimaryKey(cusId);
        // 如果id不为空，则查询交往记录
        if (null != id) {
            // 通过id查询交往记录
            CustomerContact contact = customerContactService.selectByPrimaryKey(id);
            // 将交往记录存到作用域中
            model.addAttribute("contact",contact);
        }
        model.addAttribute("customer",customer);
        return "customer/customer_contact_update";
    }

    /**
     * 添加交往记录
     * @param customerContact
     * @return
     */
    @LogAnno(description = "添加交往记录")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addCustomerContact(CustomerContact customerContact) {
        customerContactService.addCustomerContact(customerContact);
        return success("添加交往记录成功！");
    }

    /**
     * 修改交往记录
     * @param customerContact
     * @return
     */
    @LogAnno(description = "修改交往记录")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCustomerContact(CustomerContact customerContact) {
        customerContactService.updateCustomerContact(customerContact);
        return success("修改交往记录成功！");
    }

    /**
     * 删除客户交往记录
     * @param id
     * @return
     */
    @LogAnno(description = "删除客户交往记录")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomerContact(Integer id) {
        customerContactService.deleteCustomerContact(id);
        return success("删除交往记录成功！");
    }
}
